<?php
require_once('AbstractController.php');
include_once ("arc/ARC2.php");
/**
 * AuthorController.php
 *
 * @author		b.wenneker <b.wenneker@gmail.com>, 2010
 */
class AuthorController extends Thesis_AbstractController
{

	public function overviewAction ()
	{
		$store = $this->_getStore();
		$query = 'select ?name where {
				?a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .
				?a <http://xmlns.com/foaf/0.1/name> ?name
			} order by ?name';
		
		$rows = $store->query($query, 'rows');
		$authorCounts = array_fill_keys(range('a', 'z'), 0);
		foreach ((array) $rows as $row) {
			$name = lcfirst(ltrim($row['name']));
			$authorCounts[$name[0]] += 1;
		}
		
		$this->view->assign('authorCounts', $authorCounts);
	}
	
	public function authorsAction ()
	{
		
		$character = $this->getRequest()->getParam('queryparam');
		
		if($this->_contentType == 'page'){
			$store = $this->_getStore();
			$query =
			  'PREFIX foaf: <http://xmlns.com/foaf/0.1/>
			  PREFIX dc: <http://purl.org/dc/elements/1.1/>
			  PREFIX dcterms: <http://purl.org/dc/terms/>
			  
			  SELECT DISTINCT ?name FROM <http://wisserver.st.ewi.tudelft.nl> WHERE {
				?a ?b foaf:Person .
				?a foaf:name ?name
				FILTER regex(?name, "^' . $character . '", "i")
			  } ORDER BY ?name';
			
			$rows = $store->query($query, 'rows');
			$authors = array();
			
			foreach ($rows as $row) {
				$authors[] = $row['name'];
			}
			
			$this->view->assign('character', $character);
			$this->view->assign('authors', $authors);
		}else if($this->_contentType == 'data'){
			// User agent requests rdf data.
			
		}
	}
	
	public function indexAction ()
	{
		
		$author = $this->getRequest()->getParam('queryparam', 'no_author');
		if($this->_contentType == 'page'){
			$store = $this->_getStore();
			
			$query =
				'PREFIX dc: <http://purl.org/dc/elements/1.1/>
		  			SELECT ?title WHERE {
					?d <http://xmlns.com/foaf/0.1/maker> <http://wisserver.st.ewi.tudelft.nl/resource/author/'.urlencode($author).'> .
					?d dc:title ?title .
		   		}';
			
			$rows = $store->query($query, 'rows');
			$titles = array();
			
			foreach ($rows as $row) {
				$titles[] = $row['title'];
			}
			
			$this->view->assign('author', $author);
			$this->view->assign('titles', $titles);
		}else if($this->_contentType == 'data'){
			// User agent requests rdf data.
			$config = array('remote_store_endpoint' => 'http://wisserver.st.ewi.tudelft.nl:8893/sparql');
			$ssp = ARC2::getSPARQLScriptProcessor($config);

			/* script evaluation */
			$scr = '
			  DESCRIBE ?property ?hasValue ?isValueOf
				WHERE {
				  { <http://wisserver.st.ewi.tudelft.nl/authors/'.str_replace(' ','_',$author).'> ?property ?hasValue }
				  UNION
				  { ?isValueOf ?property <http://wisserver.st.ewi.tudelft.nl/authors/'.str_replace(' ','_',$author).'> }
				}
			';
			
			$scr = urlencode($scr);
			$contents = file_get_contents('http://wisserver.st.ewi.tudelft.nl:8893/sparql?default-graph-uri=&query='.$scr.'&format=auto');

			header('Content-Type: application/rdf+xml; charset=utf-8');
			header('Content-Disposition: attachment; filename="'.$author.'.rdf"');
			echo $contents;
			
			exit();
		}
	}
	
	public function graphAction(){
	
		$store = $this->_getStore();
		$author = $this->getRequest()->getParam('queryparam', 'no_author');
		
		$store = $this->_getStore();
		$query = '
			PREFIX dc: <http://purl.org/dc/elements/1.1/>
			PREFIX foaf: <http://xmlns.com/foaf/0.1/>
			PREFIX dcterms: <http://purl.org/dc/terms/>
			SELECT ?date WHERE {
				?d foaf:maker <http://wisserver.st.ewi.tudelft.nl/resource/author/'.urlencode($author).'> .
				?d dcterms:issued ?date FILTER(!isblank(?date)). 
			} ORDER BY ?date';

		$previous_date = '';
		if ($rows = $store->query($query, 'rows')) {
			foreach ($rows as $row) {
				$new_date = $row['date'];		
				if($previous_date != $new_date){
					$count = 1;
					$dates[$new_date] = $count;		
				}
				else{
					$dates[$previous_date] = $count++;
				}
				$previous_date = $new_date;
			}
		}
		
		$config = array('remote_store_endpoint' => 'http://wisserver.st.ewi.tudelft.nl:8893/sparql');
		$store = ARC2::getRemoteStore($config);
		$query = '
			PREFIX dc: <http://purl.org/dc/elements/1.1/>
			PREFIX foaf: <http://xmlns.com/foaf/0.1/>
			PREFIX dcterms: <http://purl.org/dc/terms/>
			SELECT ?date WHERE {
				<http://wisserver.st.ewi.tudelft.nl/resource/author/'.urlencode($author).'> <http://www.w3.org/2002/07/owl#sameAs> ?dblp_uri.
				?d foaf:maker ?dblp_uri .
				?d dcterms:issued ?date . 
			} ORDER BY ?date';
		
		$previous_date = '';
		if ($rows = $store->query($query, 'rows')) {
			foreach ($rows as $row) {
				$date = explode('-',$row['date']);
				$new_date = $date[0];		
				if($previous_date != $new_date){
					$count = 1;
					$dates_dblp[$new_date] = $count;		
				}
				else{
					$dates_dblp[$previous_date] = $count++;
				}
				$previous_date = $new_date;
			}
		}
		
		$this->view->assign('author', $author);
		$this->view->assign('dates', $dates);
		$this->view->assign('datesdblp', $dates_dblp);
	}
	
	public function worldmapAction(){
	
		$store = $this->_getStore();
		$author = $this->getRequest()->getParam('queryparam', 'no_author');
		
		$store = $this->_getStore();
		$q = '
		  SELECT ?location WHERE {
			?d <http://xmlns.com/foaf/0.1/maker> <http://wisserver.st.ewi.tudelft.nl/resource/author/'.urlencode($author).'> .
			?d <http://dbpedia.org/ontology/Place> ?e . 
			?e <http://www.w3.org/2002/07/owl#sameAs> ?location . 
		   }';

		if ($rows = $store->query($q, 'rows')) {
			foreach ($rows as $row) {
				$locations[] = $row['location'];	
			}
		}

		foreach((array)$locations as $location){
			$store = $this->_getStore(8892);

			$q = '
			  SELECT ?code WHERE {
				<'.$location.'> <http://www.geonames.org/ontology#inCountry> ?code .
			  }';

			if ($rows = $store->query($q, 'rows')) {		
				foreach ($rows as $row) {
					$country = explode('#',$row['code']);
					if(!IsSet($codes[$country[1]])){
						$codes[$country[1]] = 1;
					}
					else{
						$codes[$country[1]] += 1;
					}
				}
			}
		}
		
		$this->view->assign('author', $author);
		$this->view->assign('locations', $locations);
		$this->view->assign('codes', $codes);
	}
	
	public function topicsAction(){
	
		$store = $this->_getStore();
		$author = $this->getRequest()->getParam('queryparam', 'no_author');
		
		$store = $this->_getStore();
		$q = '
		  SELECT ?topic WHERE {
			<http://wisserver.st.ewi.tudelft.nl/resource/author/'.urlencode($author).'> <http://swrc.ontoware.org/ontology#topic> ?topic .
		   }';
		
		if ($rows = $store->query($q, 'rows')) {
			foreach ($rows as $row) {
				$topics[] = $row['topic'];	
			}
		}
		
		$this->view->assign('author', $author);
		$this->view->assign('topics', $topics);
	}
	
	public function authorspertopicAction(){
	
		$store = $this->_getStore();
		$author = $this->getRequest()->getParam('queryparam', 'no_author');
		$topic = $this->getRequest()->getParam('topicparam', 'no_topic');
		
		$store = $this->_getStore();
		$q = '
		  SELECT ?x WHERE {
			?x <http://swrc.ontoware.org/ontology#topic> ?d FILTER regex(?d, "'.$topic.'", "i") .
		}';
		
		if ($rows = $store->query($q, 'rows')) {
			foreach ($rows as $row) {
				$authors[] = $row['x'];	
			}
		}
		
		$this->view->assign('author', $author);
		$this->view->assign('topic', $topic);
		$this->view->assign('authors', $authors);
	}
	
	public function coauthorsAction(){
	
		$store = $this->_getStore();
		$author = $this->getRequest()->getParam('queryparam', 'no_author');
		
		$store = $this->_getStore();
		$q = '
		  SELECT ?coauthors WHERE {
			<http://wisserver.st.ewi.tudelft.nl/resource/author/'.urlencode($author).'> <http://swrc.ontoware.org/ontology#co_author> ?coauthors .
		   }';
		
		if ($rows = $store->query($q, 'rows')) {
			foreach ($rows as $row) {
				$coauthors[] = $row['coauthors'];	
			}
		}
		
		$this->view->assign('author', $author);
		$this->view->assign('coauthors', $coauthors);
	}
}